package com.zhanghp.refer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanghp
 * @since 2023/10/19 9:40
 */
public class DoubleNode {
    public int value;
    public DoubleNode last;
    public DoubleNode next;

    public DoubleNode(int val){
        this.value = val;
        last = next = null;
    }

    public static DoubleNode generateDoubleLinkedList(int maxVal, int maxSize){
        int size = (int) (Math.random() * (maxSize + 1));
        if (size == 0) {
            return null;
        }
        DoubleNode head = new DoubleNode((int) (Math.random() * maxVal));
        DoubleNode pre = head;
        size --;
        while (size != 0){
            DoubleNode cur = new DoubleNode((int)(Math.random() * maxVal));
            pre.next = cur;
            cur.last = pre;
            pre = cur;
            size --;
        }
        return head;
    }

    // for test
    public static List<Integer> getDoubleListOriginOrder(DoubleNode head) {
        List<Integer> ans = new ArrayList<>();
        while (head != null) {
            ans.add(head.value);
            head = head.next;
        }
        return ans;
    }

    // for test
    public static boolean checkDoubleListReverse(List<Integer> origin, DoubleNode head) {
        DoubleNode end = null;
        for (int i = origin.size() - 1; i >= 0; i--) {
            if (!origin.get(i).equals(head.value)) {
                return false;
            }
            end = head;
            head = head.next;
        }
        for (int i = 0; i < origin.size(); i++) {
            if (!origin.get(i).equals(end.value)) {
                return false;
            }
            end = end.last;
        }
        return true;
    }



}
